//保存表格的实例对象
var sx = null;

//封装方法重载表格
function sxTable(){
    sx.reload()
}

layui.use(['table','form'], function(){
    var table = layui.table;
    var form = layui.form;

    //提交筛选查询
    form.on('submit(*)',function (data){
        var data = data.field;
        sx.reload({
            url:'/getStusByLikeIdAndName',
            where:{
                studentId:data.studentId,
                studentName:data.studentName,
                status:2
            }
        },true)
        return false
    })

    //提交数据重置为空
    form.on('submit(cz)',function (data){

        $("[name=studentId]").val(undefined)
        $("[name=studentName]").val(undefined)

        sx.reload({
            url:'/getStuOperationLogByTwo',
        },true)
        return false
    })

    //第一个实例
    sx = table.render({
        elem: '#demo'
        ,url: '/getStuOperationLogByTwo' //数据接口
        ,page:{
            layout:['count','prev','page','next','limit'],
            groups:5,
            limit:10,
            limits:[3,5,10]
        }
        , height:350
        ,cols: [
            [
                {checkbox: true},
                {field: 'studentId', title: '学号', width:200,align: 'center'},
                {field: 'studentName', title: '姓名', width:200,align: 'center'},
                {field: 'operationRemark', title: '操作备注', width:270,align: 'center'},
                {field: 'operationDateTime', title: '操作时间', width:230,align: 'center'},
                {title: '操作',toolbar:'#tooldemo',width: 200,align: 'center'}
            ]
        ],
        defaultToolbar: ['filter'],
        toolbar:'#toolbardemo',
        parseData:function (res){
            return{
                "code":res.state,
                "msg":res.msg,
                "count":res.total,
                "data":res.data
            }
        }
    })


    //监听头部
    table.on('toolbar(stuTable)',function (obj){
        var event = obj.event
        var checkStatus = table.checkStatus(obj.config.id);

        if(event == 'dc'){

            var data = checkStatus.data;
            console.log(checkStatus)
            // 判断是否选中数据
            if (checkStatus.data.length === 0) {
                layer.msg('请选择数据', {
                    icon: 2
                })
            } else {

                //浏览器下载导出文件
                table.exportFile(sx.config.id,data, 'xlsx');
                // 导出文件（发送后台导出数据）
                // $.ajax({
                //     type:'post',
                //     url:'/dcStuOperationLog',
                //     data:JSON.stringify(data),
                //     dataType:'json',
                //     contentType:'application/json;charset=utf-8',
                //     success:function (res){
                //         if (res.state == '200'){
                //             layer.msg('导出成功',{icon:6,time:1000});
                //         }
                //     }
                // })

            }

        }else if(event == 'dy'){

            // 监听打印选中行
            var checkStatus = table.checkStatus('demo')
            console.log(checkStatus)
            // 判断是否选中数据
            if (checkStatus.data.length === 0) {a
                layer.msg('请选择数据', {
                    icon: 2
                })
            } else {
                printSelect(checkStatus.data)
            }
        }
    }),

    //tool 监听工具条
    table.on('tool(stuTable)',function(obj){

        if(obj.event == 'del'){

            layer.confirm('是否确认删除',function (index){
                //发送ajax
                $.ajax({
                    type: 'post',
                    url: '/delStuOperationLog',
                    data: {"id":obj.data.logId},
                    dataType: 'json',
                    success: function (rs) {
                        if (rs.state == 200){

                            layer.msg("删除操作日志信息成功",{time: 700,icon:6},function (){
                                sxTable();
                            })
                        }else {
                            layer.msg("删除操作日志信息失败",{time: 1000,icon:5});
                        }
                    },
                    error: function (e) {
                        console.log("请求异常");
                        console.log(e);
                    }
                })
            })
        }
    })

})
